/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MidiGui;
import java.awt.*;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.font.FontRenderContext;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.TreeMap;
import java.util.Vector;
import midi.*;
import javax.swing.*;
import music.Chords.Chord;
import music.Note;
import music.RhythmStaff;
import music.Score;
import music.SpacingOptions;
import music.Staff;
import music.TimeSignature;
import music.global.CharacterMap;
import music.types.BarlineType;
import music.types.ClefType;
import music.types.MidiNote;
import music.types.NoteTypes;
import structures.Voice;
/**
 *
 * @author TJ
 */
public class Frame extends JFrame{
    private JScrollBar j;
    Keyboard key;
    public static TreeMap<String, Font> Fonts=new TreeMap<String,Font>();
    public Frame(){
        create();
        //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        
    }
    public void create() {
        //key=new Keyboard();
        this.setSize(500,500);
        j=new JScrollBar(JScrollBar.VERTICAL,0,500,0,500);
        j.addAdjustmentListener(new AdjustmentListener(){

            @Override
            public void adjustmentValueChanged(AdjustmentEvent e) {
                Frame.this.repaint();
            }
        });
        this.setLayout(new BorderLayout());
        this.add(new KeyboardPanel(), BorderLayout.CENTER);
        this.add(j,BorderLayout.EAST);
        this.setVisible(true);
    }
    class TestPanel extends JPanel{
        Font font = null;
        TestPanel() {
            loadFonts();
            
        }
        public void loadFonts() {
            File f=new File("./Fonts/Opus_Reg.ttf");
             String[][] fName=new String[][]{{"./Fonts/Opus_Reg.ttf","OpusRegular"},{"./Fonts/Opustext.ttf","OpusText"},
                 {"./Fonts/Opuschords.ttf","OpusChords"},{"./Fonts/Opuspchords.ttf","OpusPlainChords"},
                 {"./Fonts/Opusperc.ttf","OpusPercussion"},{"./Fonts/Opusspecial.ttf","OpusSpecial"}};
            try {
                for(String[] s:fName){
                    InputStream is = new FileInputStream(new File(s[0]));
                    Fonts.put(s[1],Font.createFont(Font.TRUETYPE_FONT, is));
                }
            } catch (Exception ex) {
              ex.printStackTrace();
              System.err.println(fName + " not loaded.  Using serif font.");
              font = new Font("serif", Font.PLAIN, 24);
            }
        }
        public void paintComponent(Graphics g2){
            Score s=new Score(1);
            Staff s2=new Staff(s);
            
            s.addStaff(s2);
            s2.getMeasure(0).setBarline(BarlineType.DOUBLE);
            Voice v=s2.getMeasure(0).getNotes().getVoice(1);
            v.addNoteAt(1, new Chord(NoteTypes.QUARTER,"Fm6"));
            v.addNoteAt(2, new Note(NoteTypes.QUARTER,new MidiNote(62,"D")));
            v.addNoteAt(3, new Note(NoteTypes.EIGTH,new MidiNote(64,"E")));
            v.addNoteAt(4, new Note(NoteTypes.QUARTER,new MidiNote(65,"F")));
            s2.addMeasure();
            v=s2.getMeasure(1).getNotes().getVoice(1);
            v.addNoteAt(1, new Note(NoteTypes.QUARTER,new MidiNote(67,"G")));
            v.addNoteAt(2, new Note(NoteTypes.QUARTER,new MidiNote(69,"A")));
            v.addNoteAt(3, new Note(NoteTypes.EIGTH,new MidiNote(71,"B")));
            v.addNoteAt(4, new Note(NoteTypes.QUARTER,new MidiNote(72,"C")));
            s2.addMeasure();
            v=s2.getMeasure(2).getNotes().getVoice(1);
            v.addNoteAt(1, new Note(NoteTypes.QUARTER,new MidiNote(72,"C")));
            v.addNoteAt(2, new Note(NoteTypes.QUARTER,new MidiNote(74,"D")));
            v.addNoteAt(3, new Note(NoteTypes.EIGTH,new MidiNote(76,"E")));
            v.addNoteAt(4, new Note(NoteTypes.QUARTER,new MidiNote(77,"F")));
            s2.addMeasure();
            v=s2.getMeasure(3).getNotes().getVoice(1);
            v.addNoteAt(1, new Note(NoteTypes.QUARTER,new MidiNote(79,"G")));
            v.addNoteAt(2, new Note(NoteTypes.QUARTER,new MidiNote(81,"A")));
            v.addNoteAt(3, new Note(NoteTypes.EIGTH,new MidiNote(83,"B")));
            v.addNoteAt(4, new Note(NoteTypes.QUARTER,new MidiNote(84,"C")));
            s2.addMeasure();
            v=s2.getMeasure(4).getNotes().getVoice(1);
            s.render(g2);
            
            //ClefType.PERCUSSION.render(g2, WIDTH, null, WIDTH);
            /*Graphics2D g=(Graphics2D) g2;
            g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
             for(int i=1;i<=5; i++) {
                 g.drawLine(20, i*10, 400, i*10);
             }
             Font font=Fonts.get("OpusRegular").deriveFont(40f);
             SpacingOptions s=new SpacingOptions();
             s.setLineSpacing(10);
             ClefType.PERCUSSION.render(g,50, s, 20);
             TimeSignature.COMMON_TIME.renderTime(g2, 50, 50, s);
             
             //g.drawString(""+(char0xf0ce, 50, 140);
             g.drawString(""+((char)0xf051), 50, 60);
             g.drawString(""+((char)0xf052), 50, 85);
             g.drawString(""+((char)0xf052), 50, 100);
             g.drawImage(CharacterMap.getImage(font, ""+(char)CharacterMap.G_CLEF),0,0,null);
             font=Fonts.get("OpusSpecial").deriveFont(40f);
             g.setFont(font);
             g.drawString(""+((char)0xf094), 403-g.getFontMetrics(font).charWidth(0xf094), 30);
             g.drawString(""+(char)0xf0A1, 70, 95);
             g.drawString(""+(char)0xf0F3, 70, 100);
             g.drawString(""+(char)0xf0F3, 70, 120);
             g.drawString(""+(char)0xf0F3, 70, 140);
             g.drawString(""+(char)0xf0A2, 70, 145);*/
             
        /*String[] fontNames = GraphicsEnvironment
                .getLocalGraphicsEnvironment()
                .getAvailableFontFamilyNames();
        
        //... Make vector of all fonts that can display basic chars.
        //    Vector (not newer ArrayList) is used by JComboBox.
                //... Display only fonts that have the alphabetic characters.
                
                g.setColor(Color.WHITE);
                //g.fillRect(0, 0, 500, 500);
                g.setColor(Color.black);
                int x=10,y=20;
                for(int i=0xF000; i<=0xF0FF; i++,y+=30) {
                    g.setFont(font);
                    g.drawString(""+(char)i,10,y-j.getValue());
                    g.setFont(new Font("arial",Font.PLAIN,24));
                    g.drawString(" -> "+Integer.toHexString(i), 40, y-j.getValue());
                }
                j.setMaximum(y);*/
                /*
        Vector<String> visFonts = new Vector<String>(fontNames.length);
        int y=10;
        for (String fontName : fontNames) {
            Font f = new Font(fontName, Font.PLAIN, 7);
            if (f.canDisplay(0x30)) {
                //... Display only fonts that have the alphabetic characters.
                g.setFont(f);
                g.setColor(Color.black);
                g.drawString(fontName+": abcdefghijklmno[qrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",20,y);
            } else {
                //    On my machine there are almost 20 fonts (eg, Wingdings)
                //    that don't display text.
                System.out.println("No alphabetics in " + fontName);
            }
            y+=10;
        }*/
        }
    
    }
}
